<?php

namespace framework\route;

class Router
{
    protected $routes = array();

    public function addRoutes($routes)
    {
        foreach ($routes as $route) {
            $this->addRoute($route['uri'], $route['class'], $route['method']);
        }
    }

    public function addRoute($uri, $class, $method)
    {
        $class = str_replace('/','\\',$class);
        $this->routes[$uri] = array('uri' => $uri, 'class' => $class, 'method' => $method);
    }

    public function getRoutes()
    {
        return $this->routes;
    }

    public function parser($uri)
    {
        foreach ($this->routes as $k=>$route) {
            if ($uri == $k) {
                $class = $route['class'];
                $method = $route['method'];
                break;
            }
        }

        if (empty($class) || empty($method))
            throw new \ErrorException("找不到uri: \"{$uri}\" 的解析配置");

        $ins = new $class();
        $ins->$method();
    }
}